/*
 * Adobe Camera Raw Calibrator
 * Version: $((version))
 * http://chromoholics.com/
 *
 * Copyright (c) 2007 Thomas Fors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

// =======================================================
// Configuration
// =======================================================

var scriptVersion = "$((version))";

#include "Acr.js"
#include "ColorChecker.js"
#include "Simplex.js"
#include "Display.js"
#include "ProPhotoRGBColor.js"
#include "Matrix3x3.js"
#include "Vector1x3.js"

// =======================================================
// Main Code
// =======================================================

// Save user's preferences
var rulerUnits = preferences.rulerUnits;
var nonLinearHistory = preferences.nonLinearHistory;
var numberOfHistoryStates = preferences.numberOfHistoryStates;

try {
	// Ask to change any required preferences
	var msg = ""
	if ( rulerUnits != Units.PIXELS )
		msg += "   --> Ruler Units must be set to Pixels.\n";
	if ( nonLinearHistory )
		msg += "   --> Nonlinear History must be disabled.\n";
	if ( numberOfHistoryStates < 2 )
		msg += "   --> History States must be set to 2 or more.\n";
	
	if ( msg != "" ) {
		if (confirm("ACR Calibrator needs to change your Photoshop preferences:\n\n" + msg + "\n" +
					"Select YES to allow ACR Calibrator to change your Photoshop preferences.\n" +
					"Present settings will be restored when the script exits.\n\n" +
					"Select NO to leave your preferences unchanged and exit ACR Calibrator.\n\n" +
					"Warning: if ACR Calibrator crashes unexpectedly, you will need to restore your\n" +
					"preferences manually."))
		{
			preferences.rulerUnits = Units.PIXELS;
			preferences.nonLinearHistory = false;
			if ( numberOfHistoryStates < 2 )
				preferences.numberOfHistoryStates = 2;
		} else {
			throw ("User aborted.");
		}
	}
	
	// Initialize Display Module
	var display = new Display();
	
	// Initialize ACR
	var acr = new Acr();
	acr.Initialize();
	
	// Initialize ColorChecker
	var cc = new ColorChecker();
	cc.FindPatchesFromActiveDoc();
	
	display.AddStatusLine("AcrCalibrator - Version " + scriptVersion);
	display.AddStatusLine("Copyright (c) 2007 Thomas Fors <tom@fors.net>");
	display.AddStatusLine("--------------------------------------------------------");
	display.AddStatusLine("ACR Version " + acr.version + acr.supported);
	display.AddStatusLine(display.GetActiveDocFullName());
	display.AddStatusLine("Corners: " + cc.patchCornerString);
	display.AddStatusLine("");
	display.Refresh();
	
	// Close open file after gathering the useful info from it
	acr.Close();
	
	// Perform calibration
	cc.WhiteBalance(1)
	cc.GrayAdjust();
	cc.WhiteBalance(2);
	cc.SaturationAdjust();
	cc.ShadowTintAdjust();
	cc.GreenCalibration(1);
	cc.BlueCalibration(1);
	cc.RedCalibration(1);
	cc.GreenCalibration(2);
	cc.BlueCalibration(2);
	cc.RedCalibration(2);
	cc.GreenCalibration(3);
	cc.BlueCalibration(3);
	cc.RedCalibration(3);
	
	display.DisplayStatus("Done.\rElapsed Time: " + display.GetElapsedMinutes() + " mins.  Speed: " + Math.round(acr.nOpens/display.GetElapsedMinutes()*100)/100);
	
	// Restore user's ruler units
	preferences.rulerUnits = rulerUnits;
	preferences.nonLinearHistory = nonLinearHistory;
	preferences.numberOfHistoryStates = numberOfHistoryStates;
	
} catch (e) {
	
	// Restore user's ruler units
	preferences.rulerUnits = rulerUnits;
	preferences.nonLinearHistory = nonLinearHistory;
	preferences.numberOfHistoryStates = numberOfHistoryStates;
	
	alert(e);
}
